#include "PlayerListLayer.h"
#include <tcResource/tc_resource.h>
#include <tcNetwork/tc_network.h>
#include "LoginLayer.h"
#include "GameLayer.h"

#define FONT_NAME	"Arial"
#define FONT_SIZE	24
#define LINE_SPACE	40

#define USER_PLAYING 1
#define USER_READY 0

static PlayerListLayer *sCurrentPlayerListLayer;

// callbacks
void get_user_list_callback(char **user_list, BOOL *status, int user_number) {
	//addPlayerInfoCells(user_list, status, user_number);
	//CCMessageBox("get user list callback", "Tan Chess Hint");
	if(sCurrentPlayerListLayer != NULL) {
		sCurrentPlayerListLayer->freePlayerInfo();
		sCurrentPlayerListLayer->copyPlayerInfo(user_list, status, user_number);
		sCurrentPlayerListLayer->need_refresh_list = true;
	}
}

void wait_for_challenge_callback() {
	//CCMessageBox("wait for challenge callback", "Tan Chess Hint");
	sCurrentPlayerListLayer->new_challenger = true;
}

// PlayerListLayer implementation
PlayerListLayer::PlayerListLayer(void) {
	player_count = 0;
	need_refresh_list = false;
	new_challenger = false;
	m_pItemMenu = NULL;
}
PlayerListLayer::~PlayerListLayer(void) {
	freePlayerInfo();
}

void PlayerListLayer::update(ccTime dt) {
	if(new_challenger) {
		new_challenger = false;
		CCScene* pScene = CCTransitionCrossFade::transitionWithDuration(0.5f, GameLayer::scene());
		if(pScene)
			CCDirector::sharedDirector()->replaceScene(pScene);
	}
	else if(need_refresh_list) {
		need_refresh_list = false;
		sCurrentPlayerListLayer->refreshPlayerInfoCells();	
	}
}

void PlayerListLayer::freePlayerInfo() {
	if(player_count <= 0)
		return;
	for(int i = 0; i < player_count; i++) {
		free(player_list[i]);
	}
	free(player_list);
	free(ready_list);
}

void PlayerListLayer::copyPlayerInfo(char **user_list, BOOL *status, int user_number) {
	player_list = (char **)calloc(user_number, sizeof(char *));
	ready_list = (BOOL *)calloc(user_number, sizeof(BOOL));
	for(int i = 0; i < user_number; i++) {
		player_list[i] = (char *)calloc(20, sizeof(char));
		strcpy(player_list[i], user_list[i]);
		ready_list[i] = status[i];
	}
	player_count = user_number;
}

void PlayerListLayer::refreshPlayerInfoCells() {
	CCSize s = CCDirector::sharedDirector()->getWinSize();
	if(m_pItemMenu != NULL) {
		this->removeChild(m_pItemMenu, true);
		m_pItemMenu = NULL;
	}
	m_pItemMenu = CCMenu::menuWithItems(NULL);
    for (int i = 0; i < player_count; ++i) {
        CCLabelTTF* label = CCLabelTTF::labelWithString(player_list[i], FONT_NAME, FONT_SIZE);
		ccColor3B color = {0, 0, 0};
		label->setColor(color);
        CCMenuItemLabel* pMenuItem = CCMenuItemLabel::itemWithLabel(label, this, menu_selector(PlayerListLayer::menuCallback));
        m_pItemMenu->addChild(pMenuItem, i);
		pMenuItem->setAnchorPoint(ccp(0, 0.5f));
        pMenuItem->setPosition(CCPointMake(30, (s.height - (i + 1) * LINE_SPACE)));
    }
    m_pItemMenu->setContentSize(CCSizeMake(s.width, (player_count + 1) * (LINE_SPACE)));
    m_pItemMenu->setPosition(CCPointZero);
	addChild(m_pItemMenu);
}

CCScene* PlayerListLayer::scene()
{
	CCScene *scene = CCScene::node();
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();

	{
		CCLayer *backgroundLayer = CCLayer::node();
		char path[128];
		tc_resource_get_path(TC_IMG_BG, path);
		CCSprite *background = CCSprite::spriteWithFile(path);
		background->setPosition(ccp(screenSize.width/2, screenSize.height/2));
		backgroundLayer->addChild(background, 1);
		scene->addChild(backgroundLayer);
	}

	{
		PlayerListLayer *layer = PlayerListLayer::node();
		scene->addChild(layer);
	}

	return scene;	
}

bool PlayerListLayer::init()
{
	CCLayer::init();
	this->setIsTouchEnabled(true);

	scheduleUpdate();
	return true;
}

void PlayerListLayer::onEnter() {
	CCLayer::onEnter();
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
	char path[128];

	// menu
	tc_resource_get_path(TC_IMG_LOGIN_BUTTON, path);
    CCMenuItemImage *login_button = CCMenuItemImage::itemFromNormalImage(path, path, this, menu_selector(PlayerListLayer::refreshCallBack));
    tc_resource_get_path(TC_IMG_EXIT_BUTTON, path);
	CCMenuItemImage *exit_button = CCMenuItemImage::itemFromNormalImage(path, path, this, menu_selector(PlayerListLayer::logoutCallBack));
 
    CCMenu *menu = CCMenu::menuWithItems(login_button, exit_button, NULL);

    menu->setPosition(CCPointZero);
    login_button->setPosition(CCPointMake( screenSize.width/2 - 57, 50));
    exit_button->setPosition(CCPointMake( screenSize.width/2 + 57, 50));
    addChild(menu, 1);

	tc_network_get_online_players(get_user_list_callback);
	tc_network_set_wait_for_player_callback(wait_for_challenge_callback);
	sCurrentPlayerListLayer = this;
}

void PlayerListLayer::logoutCallBack(CCObject* pSender) {
	CCScene* pScene = CCTransitionCrossFade::transitionWithDuration(0.5f, LoginLayer::scene());
	if(pScene)
		CCDirector::sharedDirector()->replaceScene(pScene);
}

void PlayerListLayer::refreshCallBack(CCObject* pSender) {
	tc_network_get_online_players(get_user_list_callback);
	sCurrentPlayerListLayer = this;
}

void PlayerListLayer::menuCallback(CCObject * pSender) {
	CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
    int nIdx = pMenuItem->getZOrder();
	if(ready_list[nIdx] == USER_PLAYING) {
		//char *warning = strcat(player_list[nIdx], "has ")
		CCMessageBox("Selected Player Is Now Available", "Tan Chess Hint");
	}
	else {
		tc_network_create_connection(player_list[nIdx]);
		CCScene* pScene = CCTransitionCrossFade::transitionWithDuration(0.5f, GameLayer::scene());
		if(pScene)
			CCDirector::sharedDirector()->replaceScene(pScene);
	}
}